﻿package work.data
{
	import flash.events.ErrorEvent;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	
	/**
	 * 加载完成事件 
	 * @eventType flash.events.Event.COMPLETE
	 */
	[Bindable]
	[Event(name="complete", type="flash.events.Event")]
	/**
	 * 加载进度事件 
	 * @eventType flash.events.ProgressEvent.PROGRESS
	 */
	[Bindable]
	[Event(name="progress", type="flash.events.ProgressEvent")]
	/**
	 * 加载XML类 
	 * @author zhangfeng
	 * 
	 */
	public class LoadXML extends EventDispatcher
	{
		private var _data:XML;
		private var xmlList:XMLList;
		private var _loaded:Boolean=false;
		/**
		 * 加载xml类
		 * @param url xml文件地址
		 * 
		 */
		public function LoadXML(url:String=null)
		{
			if(url)
			{
				load(url);
			}
		}
		
		public function get loaded():Boolean
		{
			return _loaded;
		}

		/**
		 * 加载完成后得到的数据XML 
		 * @return 
		 * 
		 */
		public function get data():XML
		{
			return _data;
		}
		
		/**
		 * 加载 
		 * @param url XML文件地址
		 * 
		 */
		public function load(url:String):void
		{
			var loader:URLLoader=new URLLoader();
			loader.addEventListener(Event.COMPLETE,onLoadCompleteHander);
			loader.addEventListener(ProgressEvent.PROGRESS,onLoadProgressHander);
			loader.addEventListener(IOErrorEvent.IO_ERROR,onLoadErrorHander);
			loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR,onLoadErrorHander);
			
			loader.load(new URLRequest(url));
		}
		
		private function onLoadCompleteHander(e:Event):void
		{
			
			var loader:URLLoader=URLLoader(e.currentTarget);
			_data=XML(loader.data);
			xmlList=_data.descendants();
			this.dispatchEvent(new Event(Event.COMPLETE));
			_loaded=true;
		}
		private function onLoadProgressHander(e:ProgressEvent):void
		{
			this.dispatchEvent(new ProgressEvent(ProgressEvent.PROGRESS,false,false,e.bytesLoaded,e.bytesTotal));
		}
		private function onLoadErrorHander(e:ErrorEvent):void
		{
			this.dispatchEvent(new ErrorEvent(ErrorEvent.ERROR,false,false,e.text,e.errorID));
		}
		
		/**
		 * 查询XML所有节点的属性 
		 * @param attributeName 属性名
		 * @param key 关键字
		 * @param fuzzy 是否模糊查询
		 * @return XMLList
		 * 
		 */
		public function searchAttribute(attributeName:String,key:String,fuzzy:Boolean=false):XMLList
		{
			var xml:XMLList;
			if(xmlList==null || xmlList.length()==0 || key==null || key.length==0)
			{
				return null;
			}

			if(fuzzy)
			{				
				xml=xmlList.(String(@[attributeName]).search(key)!=-1);
			}else
			{
				xml=xmlList.(String(@[attributeName])==key);
			}
			if(xml.length()>0)
			{
				return xml;
			}
			return null;
		}
		
		/**
		 * 查询XMLList所有节点的属性 
		 * @param xmllist 待查询的XMLList
		 * @param attributeName 属性名
		 * @param key 关键字
		 * @param fuzzy 是否模糊查询
		 * @return XMLList
		 * 
		 */
		public static function searchXMLListAttribute(xmllist:XMLList,attributeName:String,key:String,fuzzy:Boolean=false):XMLList
		{
			var xml:XMLList;
			var xmll:XMLList=xmllist.descendants();
			if(xmll==null || xmll.length()==0 || key==null || key.length==0)
			{
				return null;
			}
			if(fuzzy)
			{
				xml=xmll.(String(@[attributeName]).search(key)!=-1);
			}else
			{
				xml=xmll.(String(@[attributeName])==key);
			}
			if(xml.length()>0)
			{
				return xml;
			}
			return null;
		}
	}
}